package com.lyj.mall.controller;

import com.lyj.mall.consts.MethodConst;
import com.lyj.mall.form.ShippingForm;
import com.lyj.mall.service.ShippingService;
import com.lyj.mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;

/**
 * @author lyj
 * @date 2020/2/3 17:57
 */
@RestController
@RequestMapping(value = "/shipping")
@Slf4j
public class ShippingController extends BaseController {

    @Autowired
    private ShippingService shippingService;

    @PostMapping
    public ResponseVo add(@Valid @RequestBody ShippingForm shippingForm) {
        this.insertLog("新增收货地址", MethodConst.INSERT, "收货地址: " + shippingForm.toString());
        return shippingService.add(user.getId(), shippingForm);
    }

    @DeleteMapping(value = "/{shippingId}")
    public ResponseVo delete(@PathParam("shippingId") Integer shippingId) {
        this.insertLog("删除收货地址", MethodConst.DELETE, "shippingId: " + shippingId);
        return shippingService.delete(user.getId(), shippingId);
    }

    @PutMapping(value = "/{shippingId}")
    public ResponseVo update(@PathParam("shippingId") Integer shippingId,
                             @Valid @RequestBody ShippingForm shippingForm) {
        this.insertLog("更新收货地址", MethodConst.UPDATE, "shippingId: " + shippingId);
        return shippingService.update(user.getId(), shippingId, shippingForm);
    }

    @GetMapping
    public ResponseVo list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        this.insertLog("查询收货地址列表", MethodConst.QUERY, "userId: " + user.getId());
        return shippingService.list(user.getId(), pageNum, pageSize);
    }

}
